<?php
	/**
	 * 获取任务奖励
	 */
    class GetTaskReward extends APIBase {
		
		public $tag = "GetTaskReward";
		public $isLogin = true;
		
		public function before() {			
			return true;
		}

		public function logic() 
		{
			//奖励类型
			if (!isset($this->param['reward_flag']))
			{
				$this->returnError(401, "没有设置领取奖励的类型");
			}
			else 
			{
				$flag = (int)$this->param['reward_flag'];
				
				if ( ($flag != 0) && ($flag != 1) )
				{
					$this->returnError(402, "奖励类型错误");
				}
			}
			
			//奖励序号
			if (!isset($this->param['reward_no']))
			{
				$this->returnError(403, "没有设置领奖序号");
			}
			else
			{
				$reward_no = (int)$this->param['reward_no'];				
			}
			
			$rets = $this->data_redis->hMget( "hu:{$this->uid}", array('total_board', 'total_win', 'play_reward_record', 'win_reward_record') );
			$play_reward_record = (int)$rets['play_reward_record'];
			$win_reward_record = (int)$rets['win_reward_record'];
			$play_total = $rets['total_board'];
			$win_total = $rets['total_win'];
			$reward_money = 0;
			
			//累计局数奖励
			if ($flag == 0)
			{
				if ( ($reward_no < 0) || ($reward_no > sizeof(Game::$can_reward_play_times)) )
				{
					$this->returnError(404, "领奖序号错误");
				}
				
				//如果已经达到领奖条件，并且没有领过，则可以领奖
				if ( ($play_total >= Game::$can_reward_play_times[$reward_no]) && ( ($play_reward_record & (1 << $reward_no)) == 0) )
				{
					$reward_money = Game::$play_times_reward_array[$reward_no];					//领奖成功
					$play_reward_record = $play_reward_record | (1 << $reward_no);		//记录领奖记录
				}
				else 
				{
					$this->returnError(405, "不能领取该序号的奖励");
				}
			}
			else if ($flag == 1)	//累计胜利局数奖励
			{
				if ( ($reward_no < 0) || ($reward_no > sizeof(Game::$can_reward_win_times)) )
				{
					$this->returnError(404, "领奖序号错误");
				}
				
				//如果已经达到领奖条件，并且没有领过，则可以领奖
				if ( ($win_total >= Game::$can_reward_win_times[$reward_no]) && ( ($win_reward_record & (1 << $reward_no)) == 0) )
				{
					$reward_money = Game::$win_times_reward_array[$reward_no];					//领奖成功
					$win_reward_record = $win_reward_record | (1 << $reward_no);		//记录领奖记录
				}
				else 
				{
					$this->returnError(405, "不能领取该序号的奖励");
				}
			}
			 
			//奖励后金币数量			
			$money = $this->data_redis->hincrBy("hu:{$this->uid}", "money", $reward_money);
			$this->data_redis->hMset("hu:{$this->uid}",array("play_reward_record" => $play_reward_record, "win_reward_record" => $win_reward_record) );
			
			
			//重新获取一次任务信息------------------------------
			//$record = $this->data_redis->hMget( "hu:{$this->uid}", array('play_reward_record', 'win_reward_record') );
			//$play_reward_record = (int)$record['play_reward_record'];
			//$win_reward_record = (int)$record['win_reward_record'];
			$play_can_reward = 0;
			$win_can_reward = 0;
			
			//累计局数奖励
			for ($i = 0;  $i < sizeof(Game::$can_reward_play_times); $i++ )
			{
				//如果已经达到领奖条件，并且没有领过，则可以领奖
				if ( ($play_total >= Game::$can_reward_play_times[$i]) && ( ($play_reward_record & (1 << $i)) == 0) )
				{
					$play_can_reward |= (1 << $i);
				}
			}
				
				
			//累计胜利局数奖励
			for ($j = 0;  $j < sizeof(Game::$can_reward_win_times); $j++ )
			{
				//如果已经达到领奖条件，并且没有领过，则可以领奖
				if ( ($win_total >= Game::$can_reward_win_times[$j]) && ( ($win_reward_record & (1 << $j)) == 0) )
				{
					$win_can_reward |= (1 << $j);
				}
			}
			
			
			$result = array();
			$result['reward'] = (int)$reward_money;		//奖励金额
			$result['money'] = (int)$money;				//奖励后金币数量
			$result['play_reward_record'] = (int)$play_reward_record;
			$result['win_reward_record'] = (int)$win_reward_record;
			$result['play_can_reward'] = (int)$play_can_reward;
			$result['win_can_reward'] = (int)$win_can_reward;
			
			$this->returnData($result, 0, "领取奖励成功");
		}
		
    	public function after() {
			$this->deinitDataRedis();
		}
    }
?>